package my.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import my.bean.Tea;
import my.dao.ITeaDao;
import my.dao.lmpl.ITeaDaoImpl;
import my.dbUtil.DBUtils;
import my.service.ITeaService;

public class ITeaServiceImpl implements ITeaService {

	@Override
	public int addTea(Tea tea) throws SQLException {
		int record = 0;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			ITeaDao teaDao = new ITeaDaoImpl(conn);
			record = teaDao.addTea(tea);
			return record;
		} catch (SQLException e) {
			System.err.println("增加失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public void addTeaList(List<Tea> list) throws SQLException {
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			ITeaDao teaDao = new ITeaDaoImpl(conn);
			teaDao.addTeaList(list);
		} catch (SQLException e) {
			System.err.println("增加失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}

	}

	@Override
	public int deleteTeaById(int id) throws SQLException {
		int record = 0;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			ITeaDao teaDao = new ITeaDaoImpl(conn);
			record = teaDao.deleteTeaById(id);
			return record;
		} catch (SQLException e) {
			System.err.println("删除失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public int modifyTeaMessage(Tea tea) throws SQLException {
		int record = 0;
		Connection conn = null;
		try {
			conn = DBUtils.getConnection();
			conn.setAutoCommit(false);
			ITeaDao teaDao = new ITeaDaoImpl(conn);
			record = teaDao.modifyTeaMessage(tea);
			return record;
		} catch (SQLException e) {
			System.err.println("修改失败!尝试进行事务回滚!");
			try {
				conn.rollback();
				System.err.println("事务回滚成功!");
			} catch (SQLException e2) {
				System.err.println("事务回滚失败!");
				e.printStackTrace();
			}
			throw e;
		} finally {
			try {
				conn.setAutoCommit(true);
			} catch (Exception e2) {
				e2.printStackTrace();
			}
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Tea> findAll() {
		Connection conn = null;
		List<Tea> list = null;
		try {
			conn = DBUtils.getConnection();
			ITeaDao teaDao = new ITeaDaoImpl(conn);
			list = teaDao.findAll();
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Tea> findTeaByName(String name) {
		Connection conn = null;
		List<Tea> list = null;
		try {
			conn = DBUtils.getConnection();
			ITeaDao teaDao = new ITeaDaoImpl(conn);
			list = teaDao.findTeaByName(name);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public List<Tea> findTeaByType(int type) {
		Connection conn = null;
		List<Tea> list = null;
		try {
			conn = DBUtils.getConnection();
			ITeaDao teaDao = new ITeaDaoImpl(conn);
			list = teaDao.findTeaByType(type);
			return list;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

	@Override
	public Tea findTeaById(int id) {
		Connection conn = null;
		Tea tea = null;
		try {
			conn = DBUtils.getConnection();
			ITeaDao teaDao = new ITeaDaoImpl(conn);
			tea = teaDao.findTeaById(id);
			return tea;
		} finally {
			DBUtils.close(conn, null, null);
		}
	}

}
